<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%@ page language="java" contentType="text/html; charset=utf-8" 
import="java.util.*"
pageEncoding="utf-8"%>

<% String appName = request.getContextPath(); %>
<html >
<head>
 <meta charset="UTF-8">
<meta name="viewport"
          content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>数据处理进程监控</title>
</head>
<%-- <link rel="stylesheet" type="text/css" href="<%=appName %>/js/themes/metro-orange/easyui.css">
<script type="text/javascript" src="<%=appName %>/js/jquery.min.js"></script>
<script type="text/javascript" src="<%=appName %>/js/jquery.easyui.min.js"></script>
<link rel="stylesheet" type="text/css" href="<%=appName %>/js/themes/icon.css"> --%>
<body >

<div style="width:1600px;margin:20px auto" class="easyui-panel">
    <div style="font-size:200%;text-align:center;">数据处理进程</div>
    
    <div style="margin:30px 20px 10px 10px ;"><a href="<%=appName%>/druid" style="margin-right:40px"> 查看数据连接情况</a>  
     <a href="<%=appName%>/axis2-web/index.jsp">查看WebService wsdl</a>
     <a href="<%=request.getContextPath()%>/mvc/mydemo/logout" target="_blank" style="float:right">Logout</a> 
     </div>

    <table id="task-list" class="easyui-datagrid" title="数据处理进程" style="width:1600px;height:700px"
            data-options="rownumbers:true,
            singleSelect:true,
            url:'<%=appName%>/mvc/manage/query',
            method:'get',
            onLoadError : loadErr,
            loadFilter : loadFilter,
            toolbar:'#tb'">
        <thead>
            <tr>
                <th data-options="field:'TASKID',width:120">任务标志</th>
                <th data-options="field:'GROUPID',width:80">任务组</th>
                <th data-options="field:'JOBNAME',width:180">任务名</th>
                <th data-options="field:'CRON',width:80,align:'center',editor:{type:'textbox',options:{required:true, missingMessage:'定时器必填!'}}">定时器</th>
                <th data-options="field:'BEAN',width:100">处理单元</th>
                <th data-options="field:'R_STATUS',width:100,align:'center'">任务状态</th>
                <th data-options="field:'R_PREFIRETIME',width:180,align:'center'">上次执行时间</th>
                <th data-options="field:'R_NEXTFIRETIME',width:180,align:'center'">下次执行时间</th>
                <th data-options="field:'R_STARTTIME',width:180,align:'center'">任务启动时间</th>
                <th data-options="field:'R_ENDTIME',width:180,align:'center'">任务结束时间</th>
                <th data-options="field:'R_ACTION',width:180,align:'center',formatter:formatOper">操作</th>
            </tr>
        </thead>
    </table>
    <div id="tb" style="padding:2px 5px;">
        <!-- <a href="#" class="easyui-linkbutton" iconCls="icon-add" onclick="addTask()" style="margin-left:10px">增加定时任务</a> -->
        <a href="#" class="easyui-linkbutton" iconCls="icon-save" onclick="saveModify()">保存修改</a>
        <a href="#" class="easyui-linkbutton" iconCls="icon-reload" onclick="refresh()" style="float:right;margin-right:20px">刷新</a>
    </div>
 </div>
 <script type="text/javascript">

var editIndex = undefined;
function endEditing(){
    if (editIndex == undefined){return true}
    if ($('#task-list').datagrid('validateRow', editIndex)){
        $('#task-list').datagrid('endEdit', editIndex);
        editIndex = undefined;
        return true;
    } else {
        return false;
    }
}
function getChanges(){
    var rows = $('#dg').datagrid('getChanges');
    alert(rows.length+' rows are changed!');
}
function addTask(){
    if (endEditing()){
        var row = $('#task-list').datagrid('getSelected');
        if(row == null){
            $('#task-list').datagrid('appendRow',{});
            editIndex = $('#task-list').datagrid('getRows').length-1;
        }else{
            var insidx = $('#task-list').datagrid('getRowIndex',row) + 1;
            $('#task-list').datagrid('insertRow',{index: insidx,row:{}});
            editIndex = insidx;
        }
        
        $('#task-list').datagrid('selectRow', editIndex)
                .datagrid('beginEdit', editIndex);
    }
}

function editTask(index){
    
        editIndex = index;
        $('#task-list').datagrid('selectRow', editIndex)
        .datagrid('beginEdit', editIndex);
}

function delTask(index){
    if (endEditing()){
           $('#task-list').datagrid('deleteRow', index)
    }
}

function saveModify(){
    if (endEditing()){
        var newrows = $('#task-list').datagrid('getChanges','inserted');
        var delrows = $('#task-list').datagrid('getChanges','deleted');
        var modifyrows = $('#task-list').datagrid('getChanges','updated');
        $.ajax({
            url: "<%=appName%>/mvc/manage/modifyTask",
            type: "POST",
            dataType: "json",
            data: {
                insertrows: JSON.stringify(newrows),
                deleterows: JSON.stringify(delrows),
                modifyrows: JSON.stringify(modifyrows),
            },
            success: function(data){
                if(data.code=="0"){
                    $.messager.alert('消息',"操作成功：" ,'info');
                    $('#task-list').datagrid('reload');
                } else {
                    $.messager.alert('错误',data.message,'error');
                }
            },
            error: function(xml){
                $.messager.alert('错误',"连接服务器失败！",'error');
            }
        });
    }
    //alert(JSON.stringify(rows))
    return;
}
function refresh(){
    if (!endEditing()){
        $.messager.alert('错误',"请完成编辑并保存后，再进行刷新",'error');
        return;
    }
    var rows = $('#task-list').datagrid('getChanges');
    if(rows.length > 0){
        $.messager.confirm('警  告', '你有' + rows.length + '条记录变更后未保存，执行刷新操作后，变更将会丢失。取消刷新回去保存变更?', function(r){
            if (r){
                return;
            }else{
                $('#task-list').datagrid('reload');
            }
        });
    }else{
        $('#task-list').datagrid('reload');
    }
}
function loadFilter(data){
    if(data.code == '0') {
        $.messager.show({
            //title:'数据刷新完成',
            msg:'数据刷新完成.',
            timeout:5000,
            showType:'slide'
        });
        return data.data;
    }
    $.messager.alert('错误',data.message,'error');
    return null;
}

function formatOper(val,row,index){
    if("None" == row.R_STATUS || "NOT IN USE" == row.R_STATUS){
        return '<a href="#" onclick="stopOrStartTask(\'' + index + '\',\'start\')">启动</a>'
           + '&#12288<a href="#" onclick="editTask(\'' + index + '\',\'start\')">编辑</a>'
           /* + '&#12288<a href="#" onclick="delTask(\'' + index + '\',\'start\')">删除</a>' */
           + '&#12288<a href="#" onclick="runOnceTask(\'' + row.BEAN + '\',\'start\')">执行一次</a>';
    }else{
        return '<a href="#" onclick="stopOrStartTask(\'' + index + '\',\'stop\')">停止</a>';
    }
}
function stopOrStartTask(index,action){
     if (!endEditing()){
        $.messager.alert('错误',"请完成编辑并保存后，再执行启停操作",'error');
        return;
    }
    var rows = $('#task-list').datagrid('getChanges');
    if(rows.length > 0){
        $.messager.alert('错误',"你有新增、修改或删除的操作还没有保存，请先保存后再执行启停操作",'error');
        return;
    }
    $('#task-list').datagrid('selectRow',index);
    var row = $('#task-list').datagrid('getSelected'); 
    var url = "<%=appName%>/mvc/manage/";
    if('start' == action ) {
        url += "startTask";
    }else{
        url += "stopTask";
    }
    
    //alert(JSON.stringify(row));
    $.ajax({
        url: url,
        type: "POST",
        dataType: "json",
        data: {
            row: JSON.stringify(row)
        },
        success: function(data){
            if(data.code=="0"){
                $.messager.alert('消息',"操作成功：" + data.message ,'info');
                $('#task-list').datagrid('reload');
            } else {
                $.messager.alert('错误',data.message,'error');
            }
        },
        error: function(xml){
            $.messager.alert('错误',"连接服务器失败！",'error');
        }
    });
    
}

function runOnceTask(bean){
    var url = "<%=appName%>/mvc/manage/doTaskOnce";
    $.ajax({
        url: url,
        type: "POST",
        dataType: "json",
        data: {
            bean: bean
        },
        success: function(data){
            if(data.code=="0"){
                $.messager.alert('消息',"操作成功",'info');
                $('#task-list').datagrid('reload');
            } else {
                $.messager.alert('错误',data.message,'error');
            }
        },
        error: function(xml){
            $.messager.alert('错误',"连接服务器失败！",'error');
        }
    });
}

function loadErr(){
    $.messager.alert('错误',"loaderr",'error');
}
</script>
</body>

</html>